package com.lgb.controller.admin;

import com.lgb.Service.TypeService;
import com.lgb.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    TypeService typeService;

    //访问分类管理页面
    @GetMapping("/type_manage")
    //默认页面放5条数据，按照id排序，排序方式为倒序
    public String type_manage(@PageableDefault(size = 5 , sort = {"id"} , direction = Sort.Direction.DESC) Pageable pageable,
                              Model model){
        model.addAttribute("page" , typeService.listType(pageable));
        //page对象传递给前端的是一个json对象，具体数据格式参考说明文档
        return "admin/type_manage";
    }

    //跳转到新增分类页面
    @GetMapping("type_manage/type_create")
    public String input(Model model){
        //意义不明
        model.addAttribute("type" , new Type());
        return "admin/type_create";
    }

    //新增分类
    @PostMapping("/input")
    public String add_type(Type type , RedirectAttributes redirectAttributes){
        //查询传递而来的Name是否已经存在
        Type type1 = typeService.getTypeByName(type.getName());
        //若已经存在
        if(type1 != null){
            redirectAttributes.addFlashAttribute("message" , "新增失败,该类别已存在");
        }else {
            //接收数据并添加数据
            Type t = typeService.saveType(type);
            if (t == null) {
                redirectAttributes.addFlashAttribute("message", "新增失败");
                System.out.println("新增失败");
            } else {
                redirectAttributes.addFlashAttribute("message", "新增成功");
                System.out.println("新增成功");
            }
        }
        //重定向:重定向别加空格...........
        return "redirect:/admin/type_manage";
    }

    //跳转到修改页面
    @GetMapping("/type_manage/{id}/update")
    public String update(@PathVariable Long id , Model model){
        model.addAttribute("type" , typeService.getType(id));
        return "admin/type_create";
    }

    //修改
    @PostMapping("/update/{id}")
    public String update_type(@PathVariable Long id ,Type type , RedirectAttributes redirectAttributes){
        //查询传递而来的Name是否已经存在
        Type type1 = typeService.getTypeByName(type.getName());
        //若已经存在
        if(type1 != null){
            redirectAttributes.addFlashAttribute("message" , "修改失败,该类别已存在");
        }else {
            //接收数据并添加数据
            Type t = typeService.updateType(id,type);
            if (t == null) {
                redirectAttributes.addFlashAttribute("message", "修改失败");
                System.out.println("修改失败");
            } else {
                redirectAttributes.addFlashAttribute("message", "修改成功");
                System.out.println("修改成功");
            }
        }
        //重定向:重定向别加空格...........
        return "redirect:/admin/type_manage";
    }

    //删除
    @GetMapping("/type_manage/{id}/delete")
    public String delete(@PathVariable Long id , RedirectAttributes redirectAttributes){
        typeService.deleteType(id);
        redirectAttributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/type_manage";
    }
}
